﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Top.Api.Util;

namespace FireWall_Push
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Visible = false;
            button3.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            timer1.Enabled = true;
            listBox1.Items.Add("开始服务");
            label2.Text = "运行中";
            

        }
        public void Create_WP_HB(object Trade_JSON)
        {
            try
            {
                Top.Api.Response.TradeFullinfoGetResponse rsp = TopUtils.ParseResponse<Top.Api.Response.TradeFullinfoGetResponse>(Trade_JSON.ToString());
                //开始处理订单
                string Buyer_Nick = rsp.Trade.BuyerNick.ToString();
                string Seller_Nick = rsp.Trade.SellerNick.ToString();
                string Tid = rsp.Trade.Tid.ToString();
                int Seller_ID = Convert.ToInt32(Class.Unit.WSSM.GET_SELLER_INFO(Seller_Nick).Tables[0].Rows[0][0].ToString());

                DataSet RATE_DST = Class.Unit.WSRATE.GET_BUYER_NICK_RATE(Buyer_Nick);
                int Rate_Buyer_Count = RATE_DST.Tables[0].Rows.Count;
                if (Rate_Buyer_Count == 0)
                {
                    //列表中没有他
                }
                else
                {
                    //列表中存在这个ID 根据权限处理
                    if(Class.Unit.WSRATE.GET_FW_PUSHED(Tid)==1)
                    {
                        //已经存在 不推送
                    }
                    else
                    {
                        //不存在 推送
                        LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message("https://oapi.dingtalk.com/robot/send?access_token=1dad4e3b2ccc83c4144de8db2497d7d42bb3b3cc5d8b88d8c027f48e25a89829",
                            "预警订单\n\n订单号：" + Tid +
                            "\n下单店铺：" + rsp.Trade.SellerNick.ToString() +
                            "\n买家昵称：" + Buyer_Nick.ToString() +
                            "\n下单时间：" + rsp.Trade.Created.ToString() +
                            "\n给过同行中差评数：" + RATE_DST.Tables[0].Rows.Count.ToString() +
                            "\n给同行评价类型：" + RATE_DST.Tables[0].Rows[0][3].ToString() +
                            "\n\n内容：\n" + RATE_DST.Tables[0].Rows[0][4].ToString() +
                            "\n\n给出评价时间：" + RATE_DST.Tables[0].Rows[0][6].ToString());
                    }

                    listBox1.Items.Add("买家：" + Buyer_Nick + "给过中差评 请注意");
                    //MessageBox.Show("买家：" + Buyer_Nick + "给过中差评 请注意", "警告");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(Trade_JSON.ToString());
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                DataSet Trade_DST = Class.Unit.WSRDS.GET_RDS_LAST_TRADE();
                label4.Text = DateTime.Now.ToString();
                listBox1.Items.Add("本次需要处理" + Trade_DST.Tables[0].Rows.Count.ToString() + "条订单数据");
                for (int i = 0; i < Trade_DST.Tables[0].Rows.Count; i++)
                {
                    if (Trade_DST.Tables[0].Rows[i][1].ToString() != "TRADE_CLOSED_BY_TAOBAO" &&
                        Trade_DST.Tables[0].Rows[i][1].ToString() != "TRADE_FINISHED" &&
                        Trade_DST.Tables[0].Rows[i][1].ToString() != "TRADE_CLOSED")
                    {
                        //过滤掉以上三种类型的订单的状态
                        Thread trade = new Thread(new ParameterizedThreadStart(Create_WP_HB));
                        trade.Start(Trade_DST.Tables[0].Rows[i][8].ToString());
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            listBox1.Items.Add("停止服务");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message("https://oapi.dingtalk.com/robot/send?access_token=1dad4e3b2ccc83c4144de8db2497d7d42bb3b3cc5d8b88d8c027f48e25a89829",
                            "预警订单\n\n订单号：7412987390274939" +
                            "\n下单店铺：测试店铺" +
                            "\n买家昵称：金三胖" +
                            "\n下单时间：2017-3-4 18：46：00"+
                            "\n给过同行中差评数：2"+
                            "\n给同行评价类型：差评"+
                            "\n\n内容：\n我是金三胖 测试推送"+
                            "\n\n给出评价时间："+DateTime.Now.ToString());

        }

        public class Rootobject
        {
            public Department[] department { get; set; }
            public int errcode { get; set; }
            public string errmsg { get; set; }
        }

        public class Department
        {
            public bool autoAddUser { get; set; }
            public bool createDeptGroup { get; set; }
            public int id { get; set; }
            public string name { get; set; }
            public int parentid { get; set; }
        }

    }
}
